package ua.org.enginer.controls {
	
	import mx.controls.ComboBox;
	import mx.events.FlexEvent;

	public class ComboBox extends mx.controls.ComboBox {

		public function ComboBox() {
			super();
//			addEventListener(FlexEvent.DATA_CHANGE, function(e:*):void{
//				if (_selectedValueChanged) selectedValue = _selectedValue})
		}
		
		
		private var _valueField:String;
		public function set valueField(value:String):void {
			_valueField = value;
		}

		public function get valueField():String {
			if (!_valueField) return labelField
			return _valueField;
		}

		override public function set dataProvider(value:Object):void {
			super.dataProvider = value
			if (_selectedValueChanged) selectedValue = _selectedValue
		}
		
		private var _selectedValue:Object
		private var _selectedValueChanged:Boolean
		public function set selectedValue(value:Object):void {
			_selectedValue = value
			_selectedValueChanged = true
			for (var i:uint; i<dataProvider.length; i++) {
				if (dataProvider[i][valueField] == value) {
					selectedIndex = i
					return
				}
			}
			selectedIndex = -1
		}

		public function get selectedValue():Object {
			return selectedItem[valueField]
		}

		override public function set data(value:Object):void {
			super.data
			// if data is not in data provider
			// try set data as selectedValue
			if (selectedIndex<0) selectedValue = value
		}
	}
}

